package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

import java.util.List;

/**
 * @description: 粉丝列表
 * @author: Hupengpeng
 * @time: 2021/7/3 14:28
 */
public class FansList {
    private String open_id;
    private int cursor;
    private int count;
    private String access_token;
    private String response;

    private FansList() {
    }

    public static FansList New() {
        return new FansList();
    }

    public FansList execute() {
        String url = StrUtil.format("{}{}?open_id={}&access_token={}&cursor={}&count={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_FANS_LIST,
                getOpen_id(),
                getAccess_token(),
                getCursor(),
                getCount());
        String rs = HttpUtil.get(url);
        response = rs;
        return this;
    }

    public List<FansListResponse> convert() {
        try {
            JSONObject obj = JSONUtil.parseObj(response);
            JSONObject data = obj.getJSONObject("data");
            if (data.getInt("error_code") == 0) {
                Boolean has_more = data.getBool("has_more");
                Integer total = data.getInt("total");
                Integer cursor = data.getInt("cursor");
                List<FansListResponse> fansList = JSONUtil.toList(data.getJSONArray("list"), FansListResponse.class);
                fansList.forEach(fansListResponse -> {
                    fansListResponse.setHas_more(has_more);
                    fansListResponse.setNextCursor(cursor);
                    fansListResponse.setTotal(total);
                });
                return fansList;
            }
        } catch (Exception e) {
            throw new DouYinException(StrUtil.format("{}:{}", e.getMessage(), response));
        }
        throw new DouYinException(response);
    }

    public String getOpen_id() {
        return open_id;
    }

    public FansList setOpen_id(String open_id) {
        this.open_id = open_id;
        return this;
    }

    public int getCursor() {
        return cursor;
    }

    public FansList setCursor(int cursor) {
        this.cursor = cursor;
        return this;
    }

    public int getCount() {
        return count;
    }

    public FansList setCount(int count) {
        this.count = count;
        return this;
    }

    public String getAccess_token() {
        return access_token;
    }

    public FansList setAccess_token(String access_token) {
        this.access_token = access_token;
        return this;
    }

    public String getResponse() {
        return response;
    }

    public FansList setResponse(String response) {
        this.response = response;
        return this;
    }

    public class FansListResponse {
        private String province;
        private String union_id;
        private String avatar;
        private String city;
        private String country;
        private int gender;
        private String nickname;
        private String open_id;
        private int nextCursor;
        private boolean has_more;
        private int total;

        public String getProvince() {
            return province;
        }

        public FansListResponse setProvince(String province) {
            this.province = province;
            return this;
        }

        public String getUnion_id() {
            return union_id;
        }

        public FansListResponse setUnion_id(String union_id) {
            this.union_id = union_id;
            return this;
        }

        public String getAvatar() {
            return avatar;
        }

        public FansListResponse setAvatar(String avatar) {
            this.avatar = avatar;
            return this;
        }

        public String getCity() {
            return city;
        }

        public FansListResponse setCity(String city) {
            this.city = city;
            return this;
        }

        public String getCountry() {
            return country;
        }

        public FansListResponse setCountry(String country) {
            this.country = country;
            return this;
        }

        public int getGender() {
            return gender;
        }

        public FansListResponse setGender(int gender) {
            this.gender = gender;
            return this;
        }

        public String getNickname() {
            return nickname;
        }

        public FansListResponse setNickname(String nickname) {
            this.nickname = nickname;
            return this;
        }

        public String getOpen_id() {
            return open_id;
        }

        public FansListResponse setOpen_id(String open_id) {
            this.open_id = open_id;
            return this;
        }

        public int getNextCursor() {
            return nextCursor;
        }

        public FansListResponse setNextCursor(int nextCursor) {
            this.nextCursor = nextCursor;
            return this;
        }

        public boolean isHas_more() {
            return has_more;
        }

        public FansListResponse setHas_more(boolean has_more) {
            this.has_more = has_more;
            return this;
        }

        public int getTotal() {
            return total;
        }

        public FansListResponse setTotal(int total) {
            this.total = total;
            return this;
        }
    }
}
